<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Adjective Decliner</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="classics.css" rel="stylesheet" type="text/css">
</head>

<?php
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    $start  = $length * -1; //negative
    return (substr($haystack, $start) === $needle);
}

$nouns = array();

function addNoun($sNom, $stem, $type) {
	global $nouns;
	$nouns[$sNom]['stem'] = $stem;
	$nouns[$sNom]['type'] = $type;
}

$endings = array();

function addNEndings($type, $number, $nom, $gen, $dat, $acc, $voc, $abl) {
	global $endings;
	$endings[$type][$number] = array('nominative' => $nom, 'genitive' => $gen, 'dative' => $dat, 'accusative' => $acc, 'vocative' => $voc, 'ablative' => $abl);
}

addNEndings('1', 'singular', 'a', 'ae', 'ae', 'am', 'NULL', 'ā');
addNEndings('1', 'plural', 'ae', 'ārum', 'īs', 'ās', 'NULL', 'īs');

addNEndings('2us', 'singular', '', 'ī', 'ō', 'um', 'e', 'ō');
addNEndings('2us', 'plural', 'ī', 'ōrum', 'īs', 'ōs', 'NULL', 'īs');
addNEndings('2um', 'singular', '', 'ī', 'ō', 'um', 'NULL', 'ō');
addNEndings('2um', 'plural', 'a', 'ōrum', 'īs', 'a', 'NULL', 'īs');
addNEndings('2r', 'singular', '', 'ī', 'ō', 'um', 'NULL', 'ō');
addNEndings('2r', 'plural', 'ī', 'ōrum', 'īs', 'ōs', 'NULL', 'īs');

addNEndings('3', 'singular', '', 'is', 'ī', 'em', 'NULL', 'e');
addNEndings('3', 'plural', 'ēs', 'um', 'ibus', 'ēs', 'NULL', 'ibus');
addNEndings('3l.r.n.s_n', 'singular', '', 'is', 'ī', '', 'NULL', 'e');
addNEndings('3l.r.n.s_n', 'plural', 'a', 'um', 'ibus', 'a', 'NULL', 'ibus');
addNEndings('3is', 'singular', '', 'is', 'ī', 'em', 'NULL', 'ī');
addNEndings('3is', 'plural', 'ēs', 'ium', 'ibus', 'īs|ēs', 'NULL', 'ibus');
addNEndings('3e.al.ar', 'singular', '', 'is', 'ī', '', 'NULL', 'ī');
addNEndings('3e.al.ar', 'plural', 'ia', 'ium', 'ibus', 'ia', 'NULL', 'ibus');
addNEndings('3mixed', 'singular', '', 'is', 'ī', 'em', 'NULL', 'e');
addNEndings('3mixed', 'plural', 'ēs', 'ium', 'ibus', 'ēs|īs', 'NULL', 'ibus');

function decline($noun, $stem, $type) {
	global $endings;
	$result = array();
	foreach($endings[$type] as $number => $aNumber) {
		foreach($aNumber as $case => $ending) {
			if ($ending == '') {
				$result[$number][$case] = "'$noun'";
			} elseif ($ending == 'NULL') {
				$result[$number][$case] = 'NULL';
			} else {
				$tmpa = explode('|', $ending);
				$tmpb = array();
				foreach($tmpa as $ed) {
					$tmpb[] = $stem.$ed;
				}
				$result[$number][$case] = "'".implode('|', $tmpb)."'";
			}
		}
	}
	return $result;
}

function validateForm() {
	if (! (($_SERVER['REQUEST_METHOD'] == 'POST') && $_POST['mainadj'] && $_POST['meaning'])) {
		return False;
	}

	for($i = 1; $i <= $_POST['forms']; $i++) {
		if (! ($_POST["gender$i"] && $_POST["adj$i"] && $_POST["stem$i"])) {
			return False;
		}
	}
	return True;

}

$defaults['forms'] = '1';
$defaults['mainadj'] = '';
$defaults['meaning'] = '';

foreach(range(1,3) as $number) {
	$defaults["gender$number"]["m."] = "";
	$defaults["gender$number"]["f."] = "";
	$defaults["gender$number"]["n."] = "";
	
	$defaults["declension$number"] = $endings[0];
	$defaults["adj$number"] = "";
	$defaults["stem$number"] = '';
}


if (validateForm()) {
	$defaults['forms'] = $_POST['forms'];
	$defaults['mainadj'] =  $_POST['mainadj'];
	$defaults['meaning'] = $_POST['meaning'];

	for($i = 1; $i <= $_POST['forms']; $i++) {
		foreach($_POST["gender$i"] as $gender) {
			$defaults["gender$i"][$gender] = ' checked="checked"';
		}
		$defaults["declension$i"] = $_POST["declension$i"];
		$defaults["adj$i"] = $_POST["adj$i"];
		$defaults["stem$i"] = $_POST["stem$i"];
	}
}
?>

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function disableAdjs() {
 	var forms = document.getElementById('aforms');
 	var nFrms = forms.options[forms.selectedIndex].value;
 	var adjfrm = document.getElementById('adjfrm');
 	
 	for(var i = 1; i <= 3; i++) {
 		var decl = adjfrm.elements["declension" + i];
 		var adj = adjfrm.elements["adj" + i];
 		var stem = adjfrm.elements["stem" + i];
 		var m = document.getElementById('m.' + i);
 		var f = document.getElementById('f.' + i);
 		var n = document.getElementById('n.' + i);
 		
 		if (i > nFrms) {
 			decl.disabled = true;
 			adj.disabled = true;
 			stem.disabled = true;
 			m.disabled = true;
 			f.disabled = true;
 			n.disabled = true;
 		} else {
 			decl.disabled = false;
 			adj.disabled = false;
 			stem.disabled = false;
 			m.disabled = false;
 			f.disabled = false;
 			n.disabled = false;
 		}
 	}
}

</script>

<body onLoad="disableAdjs()">


<div id="container">
<div id="center">

<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post" id="adjfrm">
<table align="center" border=1>
<caption>Latin Adjectives Deliner<br/><br/>
Forms: <select name="forms" id="aforms" onChange="disableAdjs()">
<?php
for($i = 1; $i <= 3; $i++) {
	echo "<option value=\"$i\"";
	if ($defaults['forms'] == $i) {
		echo " SELECTED";
	}
	echo ">$i</option>\n";
}
?>
</select>
Main: <input name="mainadj" type="text" value="<?php echo $defaults['mainadj']; ?>">
Meaning: <input name="meaning" type="text" value="<?php echo $defaults['meaning']; ?>">
<br/><br/>
</caption>
<tr><td></td><td align="center">1.</td><td align="center">2.</td><td align="center">3.</td></tr>

<tr><th>Gender</th>
<?php
for($i = 1; $i <= 3; $i++) {
	echo "<td>";

	foreach($defaults["gender$i"] as $gender => $checked) {
		echo "<input type=\"checkbox\" name=\"gender$i"."[]\" id=\"$gender$i\" value=\"$gender\" $checked/>$gender\n";
	}
	echo "</td>";
}
?>
</tr>

<tr><th>Declesion</th>
<?php
for($i = 1; $i <= 3; $i++) {
	echo "<td>\n<select name=\"declension$i\">\n";


	foreach($endings as $dtype => $aDeclesion) {
		echo "<option value=\"$dtype\"";
		if ($defaults["declension$i"] == $dtype) {
			echo " SELECTED";
		}
		echo ">$dtype</option>\n";
	}
	echo "</select>\n</td>\n";
}
?>

</tr>
<tr><th>Adjective</th>
<?php
for($i = 1; $i <= 3; $i++) {
	echo '<td><input name="adj'.$i.'" type="text" value="'.$defaults["adj$i"].'"></td>'."\n";
}
?>
</tr>
<tr><th>Stem</th>
<?php
for($i = 1; $i <= 3; $i++) {
	echo '<td><input name="stem'.$i.'" type="text" value="'.$defaults["stem$i"].'"></td>'."\n";
}
?>
</tr>
<tr class="separate-above"><td align="center" colspan=4><input type="submit" value="Submit"/></td></tr>
</table>
</form>

</div>
</div>

<?php
if (validateForm()) {
	$th = "";
	$sql = "BEGIN;\n";
	for($i = 1; $i <= $_POST['forms']; $i++) {
		$decls = decline($_POST["adj$i"], $_POST["stem$i"], $_POST["declension$i"]);
		foreach($decls as $number => $aNumber) {
			$isth = ! $th;
			
			$tr[$i][$number] = "<td>".$_POST["adj$i"]."</td>";
			$tr[$i][$number] .= "<td>".$_POST["mainadj"]."</td>";
			$tr[$i][$number] .= "<td>".implode(" & ", $_POST["gender$i"])."</td>";
			$tr[$i][$number] .= "<td>$number</td>";
			$tr[$i][$number] .= "<td>".$_POST['meaning']."</td>";
			
			$sql .= "INSERT INTO adjectives VALUES('".$_POST["adj$i"]."', '".$_POST["mainadj"]."', '".implode(" & ", $_POST["gender$i"])."', ";
			$sql .= "'$number', '".$_POST['meaning']."', ";
			$sql .= implode(", ", $aNumber);
			foreach($aNumber as $case => $word) {
				if ($isth) {
					$th .= "<th>$case</th>";
				}
				$tr[$i][$number] .= "<td>$word</td>";
			}
			$sql .= ");\n";
		}
	}
	$sql .= "COMMIT;\n";
	$th = "<th>adj</th><th>mainadj</th><th>gender</th><th>number</th><th>meaning</th>$th";
?>
<hr />
<div id="container">
<div id="center">
<table align="center" border=1>
<?php
echo "<tr>$th</tr>\n";
for($i = 1; $i <= $_POST['forms']; $i++) {
	echo "<tr>".$tr[$i]['singular']."</tr>\n";
	echo "<tr>".$tr[$i]['plural']."</tr>\n";
}
?>
</table>
<hr />
<textarea align="center" style='font-size:8pt;' cols="120" rows="10" wrap="off" id="sqls" onClick="SelectAll('sqls');">
<?php echo $sql; ?>
</textarea>
</div>
</div>

<?php
}
?>
</body>
</html>
